﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oplug.CoolTower
{
    public partial class FormFilter : MetroFramework.Forms.MetroForm
    {
        public class FilterWhereEventArgs : EventArgs
        {
            public string StrWhere { get; set; }
        }
        private List<string> Columns;

        public delegate void FilterEventHandler(object sender, FilterWhereEventArgs e);
        public event FilterEventHandler FilterChanged;
        public FormFilter(List<string> columns)
        {
            InitializeComponent();
            Columns = columns;
            comboBoxCol.DataSource = Columns;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            FilterWhereEventArgs e1 = new FilterWhereEventArgs();
            
            if (comboBoxOpr.Text == "包含")
            {
                e1.StrWhere = string.Format(" WHERE CAST({0} as VARCHAR) like '%{1}%'", comboBoxCol.Text,textBoxVal.Text);
            }
            else
            {
                e1.StrWhere = string.Format(" WHERE CAST({0} as VARCHAR) = '{2}'", comboBoxCol.Text, comboBoxOpr.Text, textBoxVal.Text);
            }
            OnFilterChanged(e1);
            this.Close();
        }

        protected virtual void OnFilterChanged(FilterWhereEventArgs e)
        {
            if (FilterChanged != null)
                FilterChanged(this, e);
        }
    }
}
